package hsw.socialmedia.model.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import hsw.socialmedia.model.dao.MessageDAO;
import hsw.socialmedia.model.dao.rowMapper.MessageRowMapper;
import hsw.socialmedia.model.entity.Message;

public class MessageDAOImpl extends DAOBase implements MessageDAO {

	private final Log logger = LogFactory.getLog(MessageDAOImpl.class);
	private final MessageRowMapper messageMapper = new MessageRowMapper();
	
	@Override
	public boolean createMessage(Message message) {
	
		logger.debug("Erstellung von Message gestartet");
		
		boolean result = false;

		assert (message != null);
		// Bild fehlt noch
		String query = "INSERT INTO Message (MessageID, SenderID, ReceiverID, Text, "
				+ "Ort) VALUES " + "(?,?,?,?,?)";

		try {
			Connection con = openConnection();
			PreparedStatement prepStat = con.prepareStatement(query);

			int messageNr = getNextID(con, "Message");
			logger.debug("new Message has ID=" + messageNr);
			message.setMessageID(messageNr);

			prepStat.setInt(1, message.getMessageID());
			prepStat.setInt(2, message.getSenderID());
			prepStat.setInt(3, message.getReceiverID());
			prepStat.setString(4, message.getText());
			//prepStat.setBlob(5, message.getPicture());
			prepStat.setString(5, message.getOrt());
			prepStat.execute();
			prepStat.close();
			con.close();

			result = true;
		} catch (SQLException e) {
			logger.error("SQLException occured", e);
			result = false;
		}
		return result;
	}

	@Override
	public ArrayList<Message> getAllSendedMessages(int messageIDSender) {
		
		logger.debug("Get all sended Messages");
		Message message = null;
		ArrayList<Message> result = new ArrayList<Message>();
		
		String query = "SELECT Message.MessageID as messageID, Message.SenderID as senderID, "
				+ "Message.ReceiverID as receiverID, Message.Text as text, Message.Ort as ort "
				+ "FROM Message WHERE Message.SenderID = ?";
		
		try {
			Connection con = openConnection();
			PreparedStatement prepStat = con.prepareStatement(query);
			prepStat.setInt(1, messageIDSender);
			ResultSet rs = prepStat.executeQuery();
			// Wir erwarten mehrere Ergebnisse
			while(rs.next()){
				message = this.messageMapper.mapRow(rs);
				result.add(message);
			}
			rs.close();
			prepStat.close();
			con.close();
		} catch (SQLException e) {
			logger.error("SQLException occured", e);
			result = null;
		}
		return result;
		
	}

	@Override
	public ArrayList<Message> getAllRecievedMessages(int messageIDReceiver) {
		
		logger.debug("Get all received Messages");
		Message message = null;
		ArrayList<Message> result = new ArrayList<Message>();
		
		String query = "SELECT Message.MessageID as messageID, Message.SenderID as senderID, "
				+ "Message.ReceiverID as receiverID, Message.Text as text"
				+ "FROM Message WHERE Message.ReceiverID = ?";
		
		try {
			Connection con = openConnection();
			PreparedStatement prepStat = con.prepareStatement(query);
			prepStat.setInt(1, messageIDReceiver);
			ResultSet rs = prepStat.executeQuery();
			// Wir erwarten nur ein Ergebnis
			while(rs.next()){
				message = this.messageMapper.mapRow(rs);
				result.add(message);
			}
			rs.close();
			prepStat.close();
			con.close();
		} catch (SQLException e) {
			logger.error("SQLException occured", e);
			result = null;
		}
		return result;
	}

}
